//
//  CHRepairHeadView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHRepairHeadView.h"
#import "CHStarView.h"
@interface CHRepairHeadView()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet CHStarView *starView;

@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *levelsLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (nonatomic ,copy) NSString *imgName;
@property (nonatomic ,strong) UIImageView *imgView;
@property (nonatomic ,strong) UIButton *coverBtn;
@property (weak, nonatomic) IBOutlet UIButton *phoneNumBtn;
- (IBAction)phoneNumAction:(id)sender;



@end
@implementation CHRepairHeadView


+(instancetype)headView{
    
     return [[[NSBundle mainBundle]loadNibNamed:@"CHRepairHeadView" owner:nil options:nil]lastObject];
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [UIColor greenColor];
    self.userInteractionEnabled = YES;
    self.headImgView.userInteractionEnabled = YES;
    self.starView.starNum = 0;
    self.headImgView.layer.cornerRadius = 5;
    self.headImgView.layer.masksToBounds = YES;
    self.titleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    self.scoreLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    self.addressLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    self.levelsLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    self.nameLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    self.phoneNumBtn.titleLabel.textColor =  [UIColor colorWithHexString:@"888888" alpha:1.0f];
    self.phoneNumBtn.userInteractionEnabled=  YES;
    self.phoneNumBtn.backgroundColor = [UIColor orangeColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.headImgView addGestureRecognizer:tap];
    
 
    
}

-(void)makeCall{
    
    NSLog(@"点击了");
    
    // 第三种：通过UIWebView打电话。有提示，打完电话能自动回到应用
    UIWebView *wb = [[UIWebView alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10086"]];
    [wb loadRequest:request];
    [self addSubview:wb];

    
}



-(void)tapGesture:(UITapGestureRecognizer *)tap{

    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 创建遮罩层
    UIButton *coverBtn = [[UIButton alloc] init];
    
    //  设置遮罩层的背景颜色
    coverBtn.backgroundColor = [UIColor blackColor];
    //  设置frame
    coverBtn.frame = window.bounds;
    //  添加到控制器view中
    [window addSubview:coverBtn];
    //  给遮罩层属性赋值
    self.coverBtn = coverBtn;
    //  设置遮罩层透明度
    coverBtn.alpha = 0.0;
    
    //  让图片按钮显示在控制器view最前面 Front前面
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.imgName] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    CGRect imgViewFrm = [self.headImgView.superview convertRect:self.headImgView.frame toView:window];
    imgView.frame = imgViewFrm;

    
    [window addSubview:imgView];
    self.imgView = imgView;
//    [window bringSubviewToFront:self.headImgView];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.imgView.frame = CGRectMake(0, (UIScreenH - UIScreenW)/2, UIScreenW, UIScreenW);
        //        设置遮罩层透明度
        coverBtn.alpha = 0.5;
    }];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallImage)];
    [imgView addGestureRecognizer:tap1];
    
    //  监听遮罩层按钮的点击
    [coverBtn addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)smallImage{
    CGRect imgViewFrm = [self.headImgView.superview convertRect:self.headImgView.frame toView:nil];
    [UIView animateWithDuration:0.5 animations:^{
        //  还原图片按钮
        self.imgView.frame = imgViewFrm;
        //      设置遮罩层慢慢变透明
        self.coverBtn.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        //      当动画执行完毕就会调用这个block
        //  移除遮罩层
        [self.coverBtn removeFromSuperview];
        [self.imgView removeFromSuperview];
    }];
    
}


//-(void)setModel:(QY_Model *)model{
//    _model = model;
//    
//    self.titleLabel.text = model.name;
//    self.addressLabel.text = model.address;
//    self.nameLabel.text = model.linkman;
//    self.phoneNumLabel.text = model.linktel;
//    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:model.companyphoto]];
//    NSString *str = nil;
//    if ([model.levels isEqualToString:@"01"]) {
//        str = @"一类维修经营业务";
//    }else if([model.levels isEqualToString:@"02"]){
//        str = @"二类维修经营业务";
//    }else if([model.levels isEqualToString:@"03"]){
//        str = @"三类维修经营业务";
//    }else{
//        str = @"其他";
//    }
//    self.levelsLabel.text = str;
//    
//    
//    
//}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.headImgView.frame) +10, 10, UIScreenW - CGRectGetMaxX(self.headImgView.frame) - 7, 30);
    self.addressLabel.frame = CGRectMake(CGRectGetMaxX(self.headImgView.frame) +10, CGRectGetMaxY(self.starView.frame) +10, UIScreenW - CGRectGetMaxX(self.headImgView.frame) - 7, 30);
    
}



-(void)setCompanyid:(NSString *)companyid{
    _companyid = companyid;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:companyid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"companyid"] = companyid;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovcompanydetails" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            NSDictionary *dict = result[@"data"];
            self.imgName = dict[@"companyphoto"];
            [self.headImgView sd_setImageWithURL:[NSURL URLWithString:dict[@"companyphoto"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            self.addressLabel.text = [NSString stringWithFormat:@"地址:%@",dict[@"address"]];
            self.nameLabel.text = dict[@"linkman"];
            self.titleLabel.text = dict[@"name"];
            self.starView.starNum = [dict[@"evaluatetotle"] intValue];
            self.scoreLabel.text = [NSString stringWithFormat:@"%@ 分",dict[@"evaluatetotle"]];
            [self.phoneNumBtn setTitle:dict[@"linktel"] forState:UIControlStateNormal];
            NSString *str = nil;
            if ([dict[@"levels"] isEqualToString:@"01"]) {
                str = @"一类维修经营业务";
            }else if([dict[@"levels"] isEqualToString:@"02"]){
                str = @"二类维修经营业务";
            }else if([dict[@"levels"] isEqualToString:@"03"]){
                str = @"三类维修经营业务";
            }else{
                str = @"其他";
            }
            self.levelsLabel.text = str;
        }
    }];
    
    
    
    
    
}


- (IBAction)phoneNumAction:(id)sender {
    
    
    NSLog(@"点击了");
    
    // 第三种：通过UIWebView打电话。有提示，打完电话能自动回到应用
    UIWebView *wb = [[UIWebView alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10086"]];
    [wb loadRequest:request];
    [self addSubview:wb];

}
@end
